module.exports = {
  /**
   * 修改用户信息
   * @url user/kh/updateUser 前端调用的url参数地址
   * @description 设置当前登录用户的昵称等用户信息
   * data 请求参数 说明
   * @param {String} nickname		用户的昵称
   * @param {String} avatar 		用户的头像
   * @param {Number} gender 		用户的性别
   * res 返回参数说明
   * @param {Number} code 错误码，0表示成功
   * @param {String} msg 详细信息
   */
  main: async (event) => {
    let { data = {}, util, originalParam, userInfo } = event;
    let { uniID, vk, db } = util;
    let { uid, invite_code, username, avatar, water_amount, water_amount_back, balance } = data;
    let res = { code: 0, msg : 'ok' }
    let obj = {
      username,
      avatar,
      water_amount,
      water_amount_back,
      balance
    }
    let info
    let infoRes
    if (invite_code && !userInfo.pid) {
      infoRes = await db.collection("uni-id-users").where({ my_invite_code: invite_code }).get();
      if(infoRes.data.length >0) {
        info = infoRes.data[0];
        obj.pid = info._id
        obj.pname = info.nickname || info.username
      }
    }
    let count = 0
    if (obj.username || obj.avatar || obj.water_amount || obj.water_amount_back || obj.balance ) {
      count++
    }
    if (obj.pid) {
      count++
    }
    if (count>0) {
      res.data = await vk.baseDao.update({
        dbName: 'uni-id-users',
        whereJson: {
          _id: uid
        },
        dataJson: obj
      }, event.util);
    }
    // 业务逻辑结束-----------------------------------------------------------
    return res;
  }
}
